using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FinlogiK.FxCop.Statistics.Windows.Properties;


namespace FinlogiK.FxCop.Statistics.Windows
{
	internal sealed partial class MainForm : Form
	{
		#region Fields

		private bool _isFirstActivation = true;

		#endregion

		#region Constructors

		internal MainForm()
		{
			InitializeComponent();

			Settings.Default.DatabaseConfigurationCompleteChanged += new ValueChangedEventHandler<bool>(this.HandleDatabaseConfigurationCompleteChanged);
			this.ToggleButtonsEnabled(Settings.Default.IsDatabaseConfigurationComplete);
		}

		#endregion

		#region Properties

		internal string StatusText
		{
			get
			{
				return this.toolStripStatusLabel.Text;
			}
			set
			{
				this.toolStripStatusLabel.Text = (value ?? string.Empty).Trim();
			}
		}

		#endregion

		#region Methods

		internal void ShowAnalysisResults(int? projectId, DateTime? analysisTimestamp)
		{
			this.OpenMdiChild(new StatisticsViewerForm(projectId, analysisTimestamp));
		}

		protected override void OnActivated(EventArgs e)
		{
			base.OnActivated(e);

			// Open the database configuration dialog if the connection string is not configured.
			// (This is done from the first activation rather than the OnLoad method since
			//		the MDI parent window is not yet visible when OnLoad executes.)
			if (this._isFirstActivation)
			{
				this._isFirstActivation = false;
				if (!Settings.Default.IsDatabaseConfigurationComplete) this.OpenDatabaseConfigurationForm();
			}
		}

		private void OpenDatabaseConfigurationForm()
		{
			if (this.MdiChildren.Length == 0)
			{
				DatabaseConfigurationForm form = new DatabaseConfigurationForm();
				form.ShowDialog(this);
				form.Location = new Point(this.Left + 20, this.Top + 20);
			}
			else
			{
				MessageBoxEx.Show(Resources.CannotChangeDatabaseConfigurationWhileWindowsAreOpen, MessageBoxIcon.Error);
			}
		}

		private void OpenMdiChild(Form childForm)
		{
			Form runningInstance = null;
			foreach (Form form in this.MdiChildren)
			{
				if (form.GetType() == childForm.GetType())
				{
					runningInstance = form;
					break;
				}
			}

			if (runningInstance == null)
			{
				childForm.MdiParent = this;
				childForm.WindowState = FormWindowState.Maximized;
				childForm.Show();
			}
			else
			{
				childForm.Dispose();
				runningInstance.Activate();
			}
		}

		private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void databaseToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.OpenDatabaseConfigurationForm();
		}

		private void applicationProfilesToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.OpenMdiChild(new ProjectsForm());
		}

		private void applicationProfilesToolStripButton_Click(object sender, EventArgs e)
		{
			this.OpenMdiChild(new ProjectsForm());
		}

		private void todoPrefixesToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.OpenMdiChild(new TodoPrefixesForm());
		}

		private void todoPrefixesToolStripButton_Click(object sender, EventArgs e)
		{
			this.OpenMdiChild(new TodoPrefixesForm());
		}

		private void HandleDatabaseConfigurationCompleteChanged(object sender, ValueChangedEventArgs<bool> e)
		{
			bool configurationComplete = e.NewValue;
			this.ToggleButtonsEnabled(configurationComplete);
		}

		private void runAnalysisToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.OpenMdiChild(new AnalysisForm());
		}

		private void runAnalysisToolStripButton_Click(object sender, EventArgs e)
		{
			this.OpenMdiChild(new AnalysisForm());
		}

		private void viewStatisticsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.ShowAnalysisResults(null, null);
		}
	
		private void viewStatisticsToolStripButton_Click(object sender, EventArgs e)
		{
			this.ShowAnalysisResults(null, null);
		}
	
		private void ToggleButtonsEnabled(bool enabled)
		{
			this.applicationProfilesToolStripMenuItem.Enabled = enabled;
			this.todoPrefixesToolStripMenuItem.Enabled = enabled;
			this.runAnalysisToolStripMenuItem.Enabled = enabled;
			this.viewStatisticsToolStripMenuItem.Enabled = enabled;

			this.applicationProfilesToolStripButton.Enabled = enabled;
			this.todoPrefixesToolStripButton.Enabled = enabled;
			this.runAnalysisToolStripButton.Enabled = enabled;
			this.viewStatisticsToolStripButton.Enabled = enabled;
		}

		#endregion
	}
}